<template>
  <div class="grille-container">
    <cascader-tag v-model:tag-id="query.tagId" :tag-type="tagType" :title="'全部用户'" :first-default="false" @refresh="handlePage" />
    <div class="main-container">
      <el-form :model="query" inline label-width="80px">
        <el-form-item>
          <el-button v-permission="'user:add'" type="primary" @click="openFormModal()">添加</el-button>
          <el-button :disabled="!userIds.length > 0" @click="openTagSelect">打标签</el-button>
        </el-form-item>
        <el-form-item>
          <el-input v-model="query.mobile" placeholder="请输入手机号码" prefix-icon="Search" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()"> 查询</el-button>
          <el-button @click="resetQuery()">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="page.loading" :data="page.list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="用户信息">
          <template #default="scope">
            <div style="display: flex">
              <div>
                <el-avatar :alt="scope.row.nickname" :src="scope.row.userHead" :size="50" />
              </div>
              <div>
                <span style="margin-left: 5px">{{ scope.row.nickname }}</span>
                <br />
                <span v-if="!scope.row.tagIds" style="margin-left: 5px">无标签</span>
                <span v-else>
                  <el-tag v-for="tagId in scope.row.tagIds" :key="tagId" style="margin-left: 5px">{{ getTagName(tagId) }} </el-tag>
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="手机号码" :min-width="40" prop="mobile" />
        <el-table-column :min-width="60" label="注册信息" prop="remark">
          <template #default="scope">
            <span><enum-view :enum-name="'RegisterSource'" :enum-value="scope.row.registerSource" /></span>
            <br />
            <span>{{ scope.row.gmtCreate }}</span>
          </template>
        </el-table-column>
        <el-table-column :min-width="20" label="性别">
          <template #default="scope">
            <enum-view :enum-name="'UserSex'" :enum-value="scope.row.userSex" />
          </template>
        </el-table-column>
        <el-table-column :min-width="20" label="状态">
          <template #default="scope">
            <enum-view :enum-name="'Status'" :enum-value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column :width="160" fixed="right" label="操作" prop="address">
          <template #default="scope">
            <el-button v-permission="'user:edit'" link type="primary" @click="openFormModal(scope.row)"> 编辑 </el-button>
            <el-button v-permission="'user:edit'" v-if="scope.row.status === 0" link type="primary" @click="handleStatus(scope.row)"> 启用 </el-button>
            <el-button v-permission="'user:edit'" v-if="scope.row.status === 1" link type="primary" @click="handleStatus(scope.row)"> 禁用 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <common-pagination v-model:page="page" @pagination="handlePage" />
    </div>
  </div>
  <users-form ref="formRef" @refresh="handlePage" />
  <selector-tag v-if="selectorTagVisible" @close="handleTagSelect" />
</template>
<script setup>
  import useTable from '@/utils/table.js'
  import { ref } from 'vue'
  import { commonApi } from '@/api/common.js'
  import { useRouter } from 'vue-router'
  import CommonPagination from '@/components/Common/Pagination/index.vue'
  import EnumView from '@/components/Enum/View/index.vue'
  import UsersForm from './UsersForm.vue'
  import { ElDropdown, ElDropdownMenu, ElMessage } from 'element-plus'
  import { ArrowDown } from '@element-plus/icons-vue'
  import CascaderTag from '@/components/Cascader/Tag/index.vue'
  import SelectorTag from '@/components/Selector/Tag/index.vue'
  import { useTagStore } from '@/store/modules/tag.js'

  const tagType = ref(1)

  // 批量标签选择
  const selectorTagVisible = ref(false)
  const openTagSelect = () => {
    selectorTagVisible.value = true
  }
  const handleTagSelect = (tags) => {
    if (tags && tags.length > 0) {
      const tagIds = tags.map((item) => item.id)
      commonApi.usersTag({ userIds: userIds.value, tagIds: tagIds }).then((res) => {
        ElMessage.success(res)
        handlePage()
      })
    }
    selectorTagVisible.value = false
  }

  // 添加/修改
  const formRef = ref()
  const openFormModal = (item) => {
    formRef.value.onOpen(item)
  }

  // 查看数据
  const router = useRouter()
  const toUserRecord = function (row, tabName) {
    router.push({ path: '/users/record', query: { userId: row.id, activeName: tabName } })
  }

  const getTagName = (tagId) => {
    return useTagStore().getMap.get(tagId)
  }

  // 基础功能
  const { page, handlePage, query, handleQuery, resetQuery, handleStatus } = useTable({
    page: commonApi.usersPage,
    status: commonApi.usersUpdateStatus
  })

  // 学习记录导出
  const userIds = ref([])
  const studyRecordExportParams = ref({ ...query })
  const handleSelectionChange = (val) => {
    userIds.value = []
    userIds.value = val.map((item) => item.id)
    studyRecordExportParams.value = { ...query, userIds: userIds }
  }
</script>
